﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EbbinghausWord.Common;
using EbbinghausWord.Entities;
using System.Linq;


namespace EbbinghausWord.BLL
{
    /// <summary>
    /// 队列管理
    /// </summary>
    public static class WordQueueManager
    {
        #region private field
        private static List<WordEntity> _queue = new List<WordEntity>();
        #endregion

        #region public method
        /// <summary>
        /// 将列表中的元素添加到队列中
        /// </summary>
        /// <param name="wordList"></param>
        [Obsolete]
        public static void AddToQueue(this List<WordEntity> wordList)
        {
            foreach (var wordEntity in wordList)//.OrderBy(x => x.OrderRandom))
            {
                _queue.Enqueue(wordEntity);
            }
        }

        /// <summary>
        /// 将列表中指定数量的元素添加到队列中，不足count则添加最大数量
        /// </summary>
        /// <param name="wordList"></param>
        /// <param name="count"></param>
        public static void AddToQueue(this List<WordEntity> wordList, int count)
        {
            var et = new ExecuteTimer(false);

            var takeWordList = wordList.Take(count);
            et.Show("takeWordList");

            foreach (var wordEntity in takeWordList)
            {
                et.Show("BeforeEnqueue");
                _queue.Enqueue(wordEntity);
                et.Show("EndEnqueue");
            }

        }

        /// <summary>
        /// 入队
        /// </summary>
        /// <param name="wordEntity"></param>
        /// <returns></returns>
        public static void Enqueue(WordEntity wordEntity)
        {
            _queue.Enqueue(wordEntity);
        }

        /// <summary>
        /// 指定位置 入队
        /// </summary>
        /// <param name="wordEntity"></param>
        /// <param name="index">索引从0开始 </param>
        public static void Enqueue(WordEntity wordEntity, int index)
        {
            _queue.Enqueue(wordEntity, index);
        }

        /// <summary>
        /// 出队，如果队列中没有元素，则返回null
        /// </summary>
        public static WordEntity Dequeue()
        {
            return _queue.Count > 0 ? _queue.Dequeue() : null;
        }

        /// <summary>
        /// 返回队列的第一个元素，但是该元素不出队列 
        /// </summary>
        /// <returns></returns>
        public static WordEntity Peek()
        {
            return _queue.Count > 0 ? _queue.Peek() : null;
        }

        /// <summary>
        /// 初始化队列
        /// </summary>
        public static void InitQueue()
        {
            _queue = IsExistSerrielizeQueue() ? LoadSerielizedQueueFile() : new List<WordEntity>();
        }

        /// <summary>
        /// 队列中是否包含某个成员
        /// </summary>
        /// <returns></returns>
        public static bool Contains(WordEntity wordEntity)
        {
            return _queue.Contains(wordEntity);
        }


        #endregion

        #region 队列持久化操作

        /// <summary>
        /// 是否存在 持久化的队列
        /// </summary>
        /// <returns></returns>
        private static bool IsExistSerrielizeQueue()
        {
            return !string.IsNullOrEmpty(new UserConfig().SerielizeQueueWord);
        }

        ///<summary>
        ///加载持久化的队列单词，建立与用户单词本的引用关系，并删除队列文件
        ///</summary>
        ///<returns></returns>
        private static List<WordEntity> LoadSerielizedQueueFile()
        {
            List<WordEntity> queue = null;
            try
            {
                queue = new List<WordEntity>
                            {
                                //队列中加入持久化的单词
                                WordBookManager.GetWordByEnglish(new UserConfig().SerielizeQueueWord)
                            };
            }
            catch (Exception)
            {
                queue = new List<WordEntity>();
            }

            //删除该持久化单词
            new UserConfig().SerielizeQueueWord = string.Empty;

            return queue;
        }

        /// <summary>
        /// 持久化队列
        /// </summary>
        public static void SerielizeQueue()
        {
            var word = Peek();
            if (word != null)
            {
                new UserConfig().SerielizeQueueWord = word.English;
            }
        }

        /// <summary>
        /// 清空队列缓存
        /// </summary>
        public static void ClearQueue()
        {
            new UserConfig().SerielizeQueueWord = string.Empty;
        }

        #endregion

        #region public property

        /// <summary>
        /// 队列所包含的成员数量
        /// </summary>
        public static int Count
        {
            get { return _queue.Count; }
        }

        /// <summary>
        /// 队列文件
        /// </summary>
        public static List<WordEntity> Queue
        {
            get
            {
                return _queue;
            }
        }

        #endregion

        #region entension method

        /// <summary>
        /// 入队
        /// </summary>
        /// <param name="list"></param>
        /// <param name="wordEntity"></param>
        public static void Enqueue(this List<WordEntity> list, WordEntity wordEntity)
        {
            list.Add(wordEntity);
        }

        /// <summary>
        /// 指定位置 入队
        /// </summary>
        /// <param name="list"></param>
        /// <param name="wordEntity"></param>
        /// <param name="index">索引从0开始</param>
        public static void Enqueue(this List<WordEntity> list, WordEntity wordEntity, int index)
        {
            if (list.Count <= index)
            {
                list.Add(wordEntity);
            }
            else
            {
                list.Insert(index, wordEntity);
            }
        }

        /// <summary>
        /// 出队
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static WordEntity Dequeue(this List<WordEntity> list)
        {
            var result = list[0];
            list.RemoveAt(0);
            return result;
        }

        /// <summary>
        /// 获得队列第一个元素
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static WordEntity Peek(this List<WordEntity> list)
        {
            return list[0];
        }
        #endregion
    }
}
